package org.liangsy.client.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserOperationLogServiceImpl extends ServiceImpl<UserOperationLogMapper, UserOperationLog> implements UserOperationLogService {

    @Autowired
    private UserOperationLogMapper  userOperationLogMapper;
    // 可以在此实现自定义的业务方法

    @Override
    public void weblogOperation(Integer operationType, String userId, Result result,String ipAddress) {
        Integer web = ClientLoginConstans.CLIENT_TYPE.WEB;
        saveLog(operationType, userId, result, ipAddress, web);
    }

    @Override
    public void wxLogOperation(Integer operationType, String userId, Result result,String ipAddress) {
        Integer type = ClientLoginConstans.CLIENT_TYPE.MINI_PROGRAM;
        saveLog(operationType, userId, result, ipAddress, type);
    }

    @Override
    public void appLogOperation(Integer operationType, String userId, Result result,String ipAddress) {
        Integer type = ClientLoginConstans.CLIENT_TYPE.ANDROID;
        saveLog(operationType, userId, result, ipAddress, type);
    }

    private void saveLog(Integer operationType, String userId, Result result, String ipAddress, Integer web) {
        UserOperationLog log = new UserOperationLog();
        log.setOperationType(operationType);
        log.setUserId(userId);
        log.setIpAddress(ipAddress);
        log.setClientType(web);
        log.setResult(result.isOk()? ClientLoginConstans.LOGIN_RESULT.SUCCESS:ClientLoginConstans.LOGIN_RESULT.FAILED);
        //todo 后续做日志平台时需要设置一个值
        log.setRequestId("");
        log.setErrorCode(result.getCode());
        log.setMetadata(result.getMsg());
        log.setOperationTime(System.currentTimeMillis());
        BeanUtils.initNullPropertyNames(log);
        userOperationLogMapper.insert(log);
    }
}